//
//  GDNewsDetailsWebController.m
//  爱新奇－味道
//
//  Created by 热带雨林 on 17/2/19.
//  Copyright © 2017年 热带雨林. All rights reserved.
//

#import "GDNewsDetailsWebController.h"
#import "Masonry.h"
#import "GDNetWorkTool.h"
#import "GDDetailsWebModel.h"
#import <YYModel.h>



@interface GDNewsDetailsWebController ()

@property(nonatomic, weak)UIWebView *webView;

@property (nonatomic, strong)GDDetailsWebModel *model;

@end

@implementation GDNewsDetailsWebController

- (instancetype)initWithDOCID:(NSString *)docid{
    if(self = [super initWithNibName:nil bundle:nil]){
        
        self.docid = docid;
        
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if(self.navigationController.viewControllers.count >1){
        self.tabBarController.tabBar.hidden = YES;
    }else{
        self.tabBarController.tabBar.hidden = NO;
    }
}
- (void)viewWillDisappear:(BOOL)animated{
    self.tabBarController.hidesBottomBarWhenPushed = YES;
    self.tabBarController.tabBar.hidden = NO;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
}

- (void)setupUI{
//创建一个web填充整个界面
    UIWebView *webView = [[UIWebView alloc]init];
    self.webView = webView;
    
    [self.view addSubview:webView];
    
    [webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
}
- (void)loadData{
    
    //http://c.m.163.com/nc/article/C637G3PV000380BQ/full.html
    [[GDNetWorkTool shardNetWorkTool]getNewDetailsWebWithDOCID:_docid complete:^(id respondOBJ) {
        GDDetailsWebModel *model = [GDDetailsWebModel yy_modelWithDictionary:respondOBJ];
        self.model = model;
        //显示数据,获得请求头部
       __block NSString *htmlBodyString = model.body;
        //获得图片数组
        NSArray *imageArray = model.img;
        [imageArray enumerateObjectsUsingBlock:^(NSDictionary  *_Nonnull dic, NSUInteger idx, BOOL * _Nonnull stop) {
           //由ref src 拼接一个html
            NSString *ref = dic[@"ref"];
            NSString *src = dic[@"src"];
            NSString *html = [NSString stringWithFormat:@"<img src='%@' width = 85%% />",src];
            
            htmlBodyString = [htmlBodyString stringByReplacingOccurrencesOfString:ref withString:html];
            
        }];
        
        [self.webView loadHTMLString:htmlBodyString baseURL:nil];
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
